﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace MadPropsDemo.RecipeEditor
{
    public class Commands : CommandsBase
    {
        readonly ViewModel viewModel;
        readonly Model.IRecipeService recipeService;
        readonly DialogService dialogService = DialogService.GetInstance();

        internal Commands(ViewModel viewModel, Model.IRecipeService service)
        {
            this.viewModel = viewModel.ThrowIfNull("viewModel");
            this.recipeService = service.ThrowIfNull("service");

            AddIngredient = ForDelegate(viewModel.AddIngredient);
            RemoveIngredient = ForDelegate(viewModel.RemoveIngredient);
            MoveUp = ForDelegate(viewModel.MoveUp);
            MoveDown = ForDelegate(viewModel.MoveDown);

            Save = ForDelegate(DoSave);
            Load = ForDelegate(DoLoad);
            New = ForDelegate(DoNew);
        }

        public ICommand AddIngredient { get; private set; }
        public ICommand RemoveIngredient { get; private set; }
        public ICommand MoveUp { get; private set; }
        public ICommand MoveDown { get; private set; }

        public ICommand Save { get; private set; }
        public ICommand Load { get; private set; }
        public ICommand New { get; private set; }


        const string dialogFilter = "Recipe Files|*.RECIPE";

        void DoSave()
        {
            var dialog = new System.Windows.Forms.SaveFileDialog();
            dialog.Filter = dialogFilter;
            dialog.FileName = viewModel.Recipe.Value.Name.Value;

            if (dialogService.ShowDialog(dialog) == System.Windows.Forms.DialogResult.OK)
            {
                var dto = Mapper.ToDTO(viewModel.Recipe.Value);
                recipeService.Save(dto, dialog.FileName);

                ClearUnsavedChanges();
            }
        }

        void DoLoad()
        {
            var dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Filter = dialogFilter;

            if (dialogService.ShowDialog(dialog) == System.Windows.Forms.DialogResult.OK)
            {
                var dto = recipeService.Load(dialog.FileName);
                SetRecipe(dto);
            }
        }

        void DoNew()
        {
            var recipe = new Model.RecipeDTO { Name = "[recipe name]" };
            recipe.Ingredients.Add(new Model.IngredientDTO
            {
                Name = "[first ingredient]",
                UnitOfMeasure = "pounds",
            });
            recipe.Ingredients.Add(new Model.IngredientDTO
            {
                Name = "[second ingredient]",
                UnitOfMeasure = "tbsp",
            });

            SetRecipe(recipe);
        }

        void SetRecipe(Model.RecipeDTO dto)
        {
            viewModel.TheRecipe = Mapper.FromDTO(dto);
            viewModel.TheSelectedIngredient = viewModel.Ingredients.FirstOrDefault();
            ClearUnsavedChanges();
        }

        void ClearUnsavedChanges()
        {
            viewModel.changeTracker.Clear();
            viewModel.UnsavedChangesText.Value = "";
        }
    }
}
